angular.module('mvcApp.shop', [
    'ngRoute',
    'ngResource',
    'ngSanitize'
]).config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/shop/:shopId', {
            templateUrl: 'static/views/shop.html',
            controller: 'ShopCtrl'
        });
}]).controller('ShopCtrl', ['$scope', '$http', '$location', '$sce', 'User', '$routeParams', 'Shop', 'Category', 'Product',
    function ($scope, $http, $location, $sce, User, $routeParams, Shop, Category, Product) {
        $scope.currentPage = 'shop';
        $scope.shopId = $routeParams.shopId;
        Shop.getShop($scope.shopId)
            .then(function(shop) {
                $scope.shop = shop;
                $scope.$apply();
            }).catch(function(err) {
                alert(err);
            });
        $scope.products = [];
        $scope.productsPage = 1;
        $scope.productsTotal = 0;
        $scope.pageSize = 9;
        $scope.canLoadMoreProducts = true;
        $scope.loadProducts = function() {
            if($scope.productsPage < 1) {
                $scope.productsPage = 1;
            }
            Product.listProducts($scope.shopId, $scope.productsPage, $scope.pageSize)
                .then(function(records, total) {
                    $scope.products.addAll(records);
                    $scope.productsTotal = total;
                    if (records.length < $scope.pageSize) {
                        $scope.canLoadMoreProducts = false;
                    }
                    $scope.$apply();
                }).catch(function(err) {
                    if(err) {
                        alert(err);
                    }
                });
        };
        $scope.loadProducts();
        $scope.loadMoreProducts = function () {
            $scope.productsPage = $scope.productsPage + 1;
            $scope.loadProducts();
        };
        $scope.comments = [];
        $scope.loadComments = function () {
            Shop.getLatestShopComments($scope.shopId)
                .then(function (comments) {
                    $scope.comments = comments;
                    _.each($scope.comments, function (comment) {
                        User.getUserInfoWithCache(comment.userId)
                            .then(function (userInfo) {
                                comment.user = userInfo;
                                $scope.$apply();
                            }).catch(function (err) {
                            });
                    });
                    $scope.$apply();
                }).catch(function (err) {
                    if (err) {
                        alert(err);
                    }
                });
        };
        $scope.loadComments();
        $scope.addShopComment = _.throttle(function (title, content) {
            if (!content) {
                alert("请填入评论");
                return;
            }
            Shop.addShopComment($scope.shopId, title, content)
                .then(function () {
                    $scope.loadComments();
                }).catch(function (err) {
                    alert(err);
                });
        }, 3000);
        $scope.isCollected = false;
        $scope.loadCollectedInfo =function () {
            Shop.hasCollectedShop($scope.shopId)
                .then(function(hasCollected) {
                    $scope.isCollected = hasCollected;
                    $scope.$apply();
                }).catch(function() {});
        };
        $scope.loadCollectedInfo();
        $scope.collect = function() {
            Shop.collectShop($scope.shopId)
                .then(function() {
                    $scope.loadCollectedInfo();
                }).catch(function(err) {
                    alert(err);
                });
        };
        $scope.unCollect = function() {
            Shop.unCollectShop($scope.shopId)
                .then(function() {
                    $scope.loadCollectedInfo();
                }).catch(function(err) {
                    alert(err);
                });
        };
        $scope.showShopPage = function() {
            $scope.currentPage = 'shop';
            //$scope.$apply();
        };
        $scope.showProductPage = function (product) {
            $location.path('/product/' + product.id);
        };
        $scope.showShopFeedbackPage = function() {
            $scope.currentPage = 'feedback';
            $scope.currentFeedback = {};
            //$scope.$apply();
        };
        $scope.createFeedback = function(feedback) {
            return Shop.createShopFeedback($scope.shopId, feedback)
                .then(function() {
                    alert("投诉已提交");
                    $scope.showShopPage();
                    $scope.$apply();
                }).catch(function(err) {
                    alert(err);
                });
        };
        $(".ctrl-up").hide();
        setInterval(function () {
            var $container = $(".container.content");
            if ($container.length > 0) {
                if ($container.scrollTop() > 0) {
                    $(".ctrl-up").show();
                } else {
                    $(".ctrl-up").hide();
                }
            }
        }, 300);
    }]);